#include <iostream>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char* argv[])
{
    if(argc < 2)
    {
        std::cerr << "usage: " << argv[0] << " <file to test>" << std::endl;
        return -1;
    }
    
    int fdInput;
    if((fdInput = open(argv[1], O_RDONLY,  S_IREAD)) < 0) 
    {
        std::cerr << "error opening input file: " << strerror(errno) << std::endl;
        return 1;
    }
    
    int n;
    uint64_t last, buf;
    
    n = read(fdInput, &last, sizeof(uint64_t));
    
    while((n=read(fdInput, &buf, sizeof(uint64_t))) > 0)
    {
        if(buf < last) {
            std::cerr << "sorting was not in order: " << buf << " came after " << last << std::endl;
            return 0;
        }
        last = buf;
    }
    std::cout << "Items in file " << argv[1] << " are sorted correctly." << std::endl; 
}
